using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Resources;
namespace MicroMsg.Manager
{
	public class CustomSmileyManager
	{
		private const int TUZKI_COUNT = 16;
		private static CustomSmileyManager customSmileyManager_0;
		private static List<CustomSmiley> tuzkiSmileys;
		private static string[] tuzki_MD5_arr = new string[]
		{
			"C0059FA4F781A2A500EC03FADE10E9B1",
			"E6F269A19FF2FB61FDB847B39C86EBCA",
			"D629CB3C544FB719405F2B9F16ED6E6C",
			"E2E2E96798BFBD55B35C3111D89B2E17",
			"D13E21BE9FD71777F727E0C34B0D3994",
			"68F9864CA5C0A5D823ED7184E113A4AA",
			"1483CE786912099E29551915E0BC2125",
			"BB82CE58F5ED6FDD2B5E34FC2A8E347A",
			"31574013280AAC3897722CC7E3E49EE4",
			"A00D1DE64298D9EAA145EC848A9CC8AF",
			"6257411B26D5AA873762490769625BB9",
			"5A7FC462D63EF845E6D99C1523BBC91E",
			"72EBFA527ADD152C6872219044B151C3",
			"6A9284BC5CE0BF059375E970A49FA2C5",
			"6AE79B62BAB61132981F1E85AD7070C4",
			"AAB84584B5A3F262286CB38BB107B53E"
		};
		public static string[] game_MD5_arr = new string[]
		{
			"514914788FC461E7205BF0B6BA496C49",
			"F790E342A02E0F99D34B316547F9AEAB",
			"091577322C40C05AA3DD701DA29D6423",
			"DA1C289D4E363F3CE1FF36538903B92F",
			"9E3F303561566DC9342A3EA41E6552A6",
			"DBCC51DB2765C1D0106290BAE6326FC4",
			"9A21C57DEFC4974AB5B7C842E3232671",
			"3A8E16D650F7E66BA5516B2780512830",
			"5BA8E9694B853DF10B9F2A77B312CC09"
		};
		private static CustomSmileyConfig customSmileyConfig;
		private CustomSmileyManager()
		{
		}
		public static CustomSmileyManager GetInstance()
		{
			if (CustomSmileyManager.customSmileyManager_0 == null)
			{
				CustomSmileyManager.customSmileyManager_0 = new CustomSmileyManager();
			}
			return CustomSmileyManager.customSmileyManager_0;
		}
		public static void SendGif(string cuUser, Stream gifStream, CustomSmiley customSmiley)
		{
			EmojiSendParam emojiSendParam = new EmojiSendParam();
			emojiSendParam.talkerName = cuUser;
			emojiSendParam.thumbPath = customSmiley.ThumbSrc;
			emojiSendParam.origStream = gifStream;
			emojiSendParam.filePath = customSmiley.ImageSrc;
			emojiSendParam.strMd5 = customSmiley.MD5;
			emojiSendParam.nImgType = ((customSmiley.Type == CustomSmileyType.Tuzki) ? 2 : 1);
			new NetSceneEmojiUploadImage().doScene(emojiSendParam, 47);
		}
		public static void SendGame(string cuUser, int gameType)
		{
			int num = new Random().Next(11) + 1;
			int num2 = new Random().Next(1523) + 1;
			double nowMilliseconds = Util.getNowMilliseconds();
			int num3;
			if (gameType == 1)
			{
				num3 = Math.Abs((num + nowMilliseconds + num2).GetHashCode()) % 3 + 1;
			}
			else
			{
				num3 = Math.Abs((num + nowMilliseconds + num2).GetHashCode()) % 6 + 4;
			}
			EmojiSendParam emojiSendParam = new EmojiSendParam();
			emojiSendParam.strMd5 = CustomSmileyManager.game_MD5_arr[num3 - 1];
			emojiSendParam.nImgType = 1;
			emojiSendParam.gameExtInfo.nGameType = ((num3 < 4) ? 1 : 2);
			emojiSendParam.gameExtInfo.strContent = string.Concat(num3);
			emojiSendParam.talkerName = cuUser;
			string gamePic = CustomSmileyManager.GetGamePic(num3);
			if (gamePic != null)
			{
				emojiSendParam.filePath = gamePic;
				Uri uri = new Uri(gamePic, 2);
				StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
				if (resourceStream != null)
				{
					emojiSendParam.origStream = resourceStream.get_Stream();
				}
			}
			new NetSceneEmojiUploadImage().doScene(emojiSendParam, 47);
		}
		public static CustomSmiley GetCustomSmileyByMd5(string MD5)
		{
			if (MD5 != null && MD5.get_Length() != 0)
			{
				MD5 = MD5.ToUpper();
				using (List<CustomSmiley>.Enumerator enumerator = CustomSmileyManager.getTuzkiSmileys().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						CustomSmiley current = enumerator.get_Current();
						if (current.MD5 != null && MD5 == current.MD5.ToUpper())
						{
							CustomSmiley result = current;
							return result;
						}
					}
				}
				using (List<CustomSmiley>.Enumerator enumerator2 = CustomSmileyManager.getCustomAddSmileys().GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						CustomSmiley current2 = enumerator2.get_Current();
						if (current2.MD5 != null && MD5 == current2.MD5.ToUpper())
						{
							CustomSmiley result = current2;
							return result;
						}
					}
				}
				return null;
			}
			return null;
		}
		public static string GetJSBPic(int contentValue, bool isSender)
		{
			string text = "Resource/Img/CustomSmiley/jsb/";
			if (contentValue >= 1 && contentValue <= 3)
			{
				string text2 = "j";
				if (contentValue == 2)
				{
					text2 = "s";
				}
				if (contentValue == 3)
				{
					text2 = "b";
				}
				string text3 = isSender ? "right" : "left";
				return string.Concat(new string[]
				{
					text,
					"jsb_",
					text2,
					"_",
					text3,
					".png"
				});
			}
			return null;
		}
		public static string GetGamePic(int contentValue)
		{
			string text = "Resource/Img/CustomSmiley/";
			string result = null;
			if (contentValue <= 3)
			{
				string text2 = "j";
				if (contentValue == 2)
				{
					text2 = "s";
				}
				if (contentValue == 3)
				{
					text2 = "b";
				}
				result = text + "jsb_" + text2 + ".png";
			}
			else
			{
				if (contentValue <= 10)
				{
					result = string.Concat(new object[]
					{
						text,
						"dice_",
						contentValue - 3,
						".png"
					});
				}
			}
			return result;
		}
		public List<CustomSmiley> getAllCustomSmiley()
		{
			List<CustomSmiley> list = new List<CustomSmiley>();
			list.Add(CustomSmiley.Jsb);
			list.Add(CustomSmiley.Dice);
			list.AddRange(CustomSmileyManager.getTuzkiSmileys());
			list.AddRange(CustomSmileyManager.getCustomAddSmileys());
			list.Add(CustomSmiley.AddAction);
			list.Add(CustomSmiley.DeleteAction);
			return list;
		}
		private static List<CustomSmiley> getTuzkiSmileys()
		{
			if (CustomSmileyManager.tuzkiSmileys == null)
			{
				CustomSmileyManager.tuzkiSmileys = new List<CustomSmiley>();
				for (int i = 1; i <= 16; i++)
				{
					string text = (i < 10) ? "icon_00" : "icon_0";
					CustomSmiley customSmiley = new CustomSmiley(CustomSmileyType.Tuzki, string.Concat(new object[]
					{
						"Resource/Img/CustomSmiley/",
						text,
						i,
						"_cover.png"
					}), string.Concat(new object[]
					{
						"Resource/Img/CustomSmiley/",
						text,
						i,
						".gif"
					}), CustomSmileyManager.tuzki_MD5_arr[i - 1]);
					CustomSmileyManager.tuzkiSmileys.Add(customSmiley);
				}
			}
			return CustomSmileyManager.tuzkiSmileys;
		}
		public static bool IsTuzki(string MD5)
		{
			if (string.IsNullOrEmpty(MD5))
			{
				return false;
			}
			string[] array = CustomSmileyManager.tuzki_MD5_arr;
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				if (text == MD5)
				{
					return true;
				}
			}
			return false;
		}
		public static void test()
		{
		}
		public static List<CustomSmiley> getCustomAddSmileys()
		{
			if (CustomSmileyManager.customSmileyConfig == null)
			{
				CustomSmileyManager.customSmileyConfig = ConfigMgr.smethod_0<CustomSmileyConfig>();
				if (CustomSmileyManager.customSmileyConfig.allCustomSmiley == null)
				{
					CustomSmileyManager.customSmileyConfig.allCustomSmiley = new List<CustomSmiley>();
				}
			}
			return CustomSmileyManager.customSmileyConfig.allCustomSmiley;
		}
		public static bool addCustomSmiley(Stream stream)
		{
			if (stream != null)
			{
				if (stream.get_Length() != 0L)
				{
					string hashString = MD5Core.GetHashString(new Random().Next(33888) + Util.getNowMilliseconds());
					CustomSmiley customSmiley = new CustomSmiley(CustomSmileyType.CustomAdd);
					customSmiley.ThumbSrc = StorageIO.getCustomImagePath() + "/" + hashString + ".jpg";
					customSmiley.ImageSrc = StorageIO.getCustomImagePath() + "/" + hashString + ".jpg";
					stream.Seek(0L, 0);
					if (!Util.saveJpeg(stream, customSmiley.ThumbSrc, 150, 150, 70))
					{
						return false;
					}
					byte[] array = StorageIO.readFromFile(customSmiley.ThumbSrc);
					string hashString2 = MD5Core.GetHashString(array);
					customSmiley.MD5 = hashString2;
					List<CustomSmiley> customAddSmileys = CustomSmileyManager.getCustomAddSmileys();
					customAddSmileys.Add(customSmiley);
					ConfigMgr.save<CustomSmileyConfig>(false);
					return true;
				}
			}
			return false;
		}
		public static bool addCustomSmiley(CustomSmiley customSmiley)
		{
			if (customSmiley == null)
			{
				return false;
			}
			if (!string.IsNullOrEmpty(customSmiley.MD5) && !string.IsNullOrEmpty(customSmiley.ThumbSrc) && !string.IsNullOrEmpty(customSmiley.ImageSrc))
			{
				List<CustomSmiley> customAddSmileys = CustomSmileyManager.getCustomAddSmileys();
				customAddSmileys.Add(customSmiley);
				ConfigMgr.save<CustomSmileyConfig>(false);
				return true;
			}
			return false;
		}
		public static bool deleteCustomSmiley(CustomSmiley customSmiley)
		{
			if (customSmiley == null)
			{
				return false;
			}
			List<CustomSmiley> customAddSmileys = CustomSmileyManager.getCustomAddSmileys();
			customAddSmileys.Remove(customSmiley);
			ConfigMgr.save<CustomSmileyConfig>(false);
			return true;
		}
	}
}
